package com.itheima.reggie.exception;

import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Conditional;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author tongchen
 * @create 2023-01-05 10:40
 */
@Slf4j
@ResponseBody
@ControllerAdvice(annotations = {RestController.class, Conditional.class})
public class GlobalException {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String>doException(SQLIntegrityConstraintViolationException ex){
        //处理异常信息
        String message = ex.getMessage();
        if(message.contains("Duplicate entry")){
            String[] s = message.split(" ");
            return R.error(s[2]+"账号已存在！");
        }
        return R.error("未知错误");
    }
    @ExceptionHandler(CustomerException.class)
    public R<String>doCustomerException(CustomerException ex){
        //处理异常信息
        String message = ex.getMessage();
        return R.error(message);
    }
}
